/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dtd.implementation;

import com.dtd.structure.IAnalyzer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author DMTQD
 */
public class Analyzer implements IAnalyzer {
    
    private Map<String, ArrayList> mapCategory;// chứa 1 list course theo category
    private Map<String, String> mapCourse;
    private Map<Integer , String> mapAttendance;

    public Analyzer() {
        mapCategory = new LinkedHashMap<>();
        mapCourse = new LinkedHashMap<>();
        mapAttendance = new LinkedHashMap<>();
    }

    @Override
    public Map<String, ArrayList> analyzeCategory(Map<String, String> cookies) {
        Document document = new Connector().getDocument(cookies);
        ArrayList<String> categoryKeys = new ArrayList<>();

        try {
            //lấy tên category
            // <td class="category name" valign="top"><a href="....">Mathematics</a></td>
            //-> <a>tên category</a> 
            for (Element categoryDOM : document.select(".category.name")) {
                String nameCategory = categoryDOM.select("a").text();
                categoryKeys.add(nameCategory);//add category vào categoryKeys
                mapCategory.put(nameCategory, new ArrayList());// tạo 1 category với 1 list course
            }

            for (int i = 0; i < categoryKeys.size(); i++) {
                Elements courseDOMs = document.select(".categorylist").get(i).select(".course.name").tagName("a");
                for (Element e : courseDOMs) {
                    String courseName = e.text();
                    mapCategory.get(categoryKeys.get(i)).add(courseName);
                    String url = e.select("a").attr("href");
                    String id = url.substring(55, url.length());
                    mapCourse.put(courseName,id);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return mapCategory;
    }

    public String getStudentName(Map<String, String> cookies) {
        Document document = new Connector().getDocument(cookies);
        return document.select(".logininfo").select("a").get(0).text();
    }

    public Map<String, String> getMapCourse() {
        return mapCourse;
    }

    @Override
    public Map<String, String> analyzerCourse(Map<String, String> cookies, String id) {
        Map<String, String> mapDetail  = new LinkedHashMap<>();
        //lấy điểm assignment
        try {
             Document document = Jsoup.connect("http://cms-hcm.fpt.edu.vn/elearning/mod/assignment/index.php?id="+id)
                               .cookies(cookies).timeout(30*1000).get();
             
             //lấy cột cuối cùng (điểm)
             Elements assDom = document.select(".cell.c5.lastcol");
             mapDetail.put("courseName", document.select(".headermain").get(0).text());
             
             if(assDom==null){
                  mapDetail.put("assigmentAvg","0");
             }else{
             double totalAssGrade = 0;
             int size;
             size = assDom.size();
             for(Element eAss : assDom){
                 totalAssGrade += CustomParser.parseDouble(eAss.text());
             }
             mapDetail.put("assigmentAvg",CustomParser.round2Decimals(totalAssGrade/size)+"" );
            
             }
             
        } catch (Exception e) {
           e.printStackTrace();
        }
        
        //lấy điểm quiz
        try {
             Elements quizDom = Jsoup.connect("http://cms-hcm.fpt.edu.vn/elearning/mod/quiz/index.php?id="+id)
                               .cookies(cookies).timeout(30*1000).get().select(".cell.c3");
             int size = quizDom.size();
             double totalQuizGrade = 0;
             
             for(Element quiz : quizDom){
                 String grade = CustomParser.tokenRemove(quiz.text()); // format grade : x / 10 => need remove.
                 totalQuizGrade += CustomParser.parseDouble(grade);
             }
             mapDetail.put("quizAvg",CustomParser.round2Decimals(totalQuizGrade/size)+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapDetail;
    }
    
    @Override
    //attendance
    public Map<Integer, String> analyzerAttendance(Map<String, String> cookies, String id){  
        
        Map<Integer, String> mapDetail  = new LinkedHashMap<>();
         try 
         {
            String attUrl = null;
            String loginUrl = "http://cms-hcm.fpt.edu.vn/elearning/course/view.php?id=" + id;
            
            //vào trang course view để lấy url của trang attendance
            Document courseViewDocument = new Connector().getAttDocument(cookies, loginUrl);
            for (Element getAttID : courseViewDocument.select(".activity.attforblock")) 
            {
                Element temp = getAttID.select("a").first();
                attUrl = temp.attr("href");//lấy link attendance của môn học
            }
            
            //vào trang attendance, sử dụng url vừa lấy
            int count = 0; //số thứ tự nội dung của attendance details
            Document attDocument = new Connector().getAttDocument(cookies, attUrl);
            for (Element attInfo : attDocument.select(".info.c1"))
            {
                String temp = attInfo.text(); 
                mapDetail.put(count, temp); // add thông tin điểm danh vào mapDetail
                //System.out.println(temp);
                count++;
            }                 
        } catch (Exception e) {
           e.printStackTrace();
        }
        return mapDetail;
    }

  
    
}
